<?php
// 公共函数
function conf($param=""){
	$C = include_once(__LIB__.'core/Common.conf.php');
	if( empty($param) ){
		return $C;
	}
	if( isset($C[$param]) ){
		return $C[$param];
	}
	return false;
}

function G($param){
	$val = '';
	if( isset($_POST[$param]) ){
		$val = $_POST[$param];
	} else if( isset($_GET[$param]) ) {
		$val = $_GET[$param];
	}
	if( !get_magic_quotes_gpc() ){
		$val = addslashes($val);
	}
	return trim($val);
}

function ajax_return($array){
	echo json_encode($array);
	exit;
}


function import($className){
	if( file_exists(__LIB__.'util/'.$className.'.class.php') ){
		require_once(__LIB__.'util/'.$className.'.class.php');
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

function M($model){
	if( file_exists(__MODEL__.$model.'.php') ){
		require_once(__MODEL__.$model.'.php');
		$className = $model.'Model';
		return class_exists($className) ? new $className() : false;
	}else{
		$obj = new Model( $model );
		return $obj;
	}
}

function C($controller){
	if( !empty($controller) ){
		$arr = explode('.',$controller);
	}else{
		return false;
	}
	$className = $arr[count($arr)-1];
	unset($arr[count($arr)-1]);
	$path = "";
	if( is_array($arr) && count($arr)>0 ){
		foreach($arr as $v){
			$path .= $v.'/';
		}
	}
	$baseClass = __CONTROLLER__.$path.'base.php';
	if( file_exists($baseClass) ) require_once( $baseClass );
	$file = __CONTROLLER__.$path.$className.'.php';
	if( file_exists($file) ){
		require_once($file);
		$className = $className.'Controller';
		return class_exists($className) ? new $className() : false;
	}else{
		return false;
	}
}

/**
 * 递归生成树状结构
 * 
 * @param $cates
 * @param $parent
 * @param $return
 * @return 
 * @since v1.0
 * @create 2012-09-20
 */
function build_tree( $cates, &$parent, &$return ){
	if( !is_array($cates) ) return false;
	foreach( $cates as $k=>$v ){
		$return[] = $v;
		if( isset($parent[$v['id']]) ){
			build_tree( $parent[$v['id']],$parent, $return );
		}
		unset($parent[$v['id']]);
	}
}


/**
 * 生成带缩进的数组
 * 
 * @param $arr 数据
 * @param $sep 缩进符号
 * @param $needCheckbox 是否带复选框: 值为false,'',0时不带，为string时，string为name属性值
 * @return 
 * @since v1.0
 * @create 2012-09-20
 */
function tree($arr, $sep='&nbsp;&nbsp;', $needCheckbox=false, $topids=''){
	$arr_parents = array();
	$new_arr = array();
	$rootNodes = is_array($topids) ? $topids : array(0);
	foreach( $arr as $v ){
		$tmp = $v;
		if( in_array($v['pid'],$rootNodes) ){
			$tmp['level'] = 0;
			$arr_parents[0][] = $tmp;
		}else{
			$arr_parents[$v['pid']][] = $tmp;
		}
		$new_arr[$v['id']] = $tmp;
	}	
	ksort($arr_parents);
	foreach( $arr_parents as $key=>$val ){
		foreach( $val as $k=>$v ){
			$pid = $arr_parents[$key][$k]['pid'];
			$level = isset($new_arr[$pid]['level']) ? $new_arr[$pid]['level']+1 : 0;
			$new_arr[$v['id']]['level'] = $level;
			$arr_parents[$key][$k]['level'] = $level;
		}
	}
	$tmp = array_slice($arr_parents,0,1);
	$key = array_keys($tmp);
	$return = array();
	build_tree( $tmp[$key[0]], $arr_parents, $return);
	foreach( $return as $k=>$v ){
		$multiplier = $v['level'] * 2;
		if( !empty($needCheckbox) ) $v['name'] = '<label><input type="checkbox" name="'.$needCheckbox.'" value="'.$v['id'].'" />'.$v['name'].'</label>';
		$return[$k]['name'] = str_repeat($sep,$multiplier).$v['name'];
	}
	return $return;
}


function alert( $msg,$url="" ){
	if( $url==-1 ){
		$url = "";
	}elseif( !empty($url) ){
		$url = 'location.href="'.$url.'";';
	}else{
		$url = 'history.back();';
	}
	echo '<script type="text/javascript">alert("'.$msg.'");'.$url.'</script>';
}

function tips( $msg, $type=1, $reload=0, $redirect="" ){
	echo '<script type="text/javascript">tips("'.$msg.'",'.$type.','.$reload.',"'.$redirect.'");</script>';
}


function substring($str, $start, $len=""){
	if( function_exists('mb_substr') ){
		return mb_substr($str, $start, $len);
	}else{
		return substr($str, $start, $len);
	}
}

function dump($var){
	ob_start();
	var_dump($var);
	$str = ob_get_clean();
	$str = preg_replace("/\]\=\>\n(\s+)/m","] => ",$str);
	echo "<pre>".htmlspecialchars($str,ENT_QUOTES)."</pre>";
}

function lang($name=''){
	if( empty($name) ){
		return __APP__.'lang/zh-cn.php';
	}
	$lang = require_once(__APP__.'lang/zh-cn.php');
	return $lang[$name];
}

function U($var, $param=''){
	if( empty($var) ) return false;
	$url = $var;
	if( is_array( $param ) ){
		foreach( $param as $k=>$v ){
			$url .= "/$k/$v";
		}
	}
	return __URL__.'index.php/'.$url;
}
